//
//  ZTRecommendViewController.m
//  XMLYFM
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 zzl. All rights reserved.
//




/**
 *  @author zzl
 *
 *  推荐页面
 *
 * TableView 嵌套collectionView 
 
 
 *
 *  @return
 */


#import "ZTRecommendViewController.h"
#import <AFNetworking.h>
#import <Masonry.h>
#import "TZRommendHeader.h"

#import "TZeditorViewCell.h"
#import "TZJIngPinViewCell.h"
#import "TZfindViewCell.h"

//热门推荐 我写错了类名
#import "TZFIndTableViewCell.h"


#import "TZreommandTou.h"
#import "ZTAlbumDetailViewController.h"
#import "AppTools.h"

//小编详情页面

#import "TZEditorViewController.h"


//发现详情界面
#import "TZfindController.h"


//精品详情界面
#import "TZjingPInDetail.h"

@interface ZTRecommendViewController ()<UITableViewDelegate,UITableViewDataSource,PicTableCellDelegate,Pic2TableCellDelegate,FIndTDeleget,JingPinDeleget>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)TZRommendHeader *headr;

//小编推荐字典
@property(nonatomic,strong)NSMutableDictionary *editor;

//精品听单字典
@property(nonatomic,strong)NSMutableDictionary *jingPin;


//发现新奇字典
@property(nonatomic,strong)NSMutableDictionary *FindNew;


//热门推荐字典
@property(nonatomic,strong)NSMutableDictionary *hotTuiJian;



@property(nonatomic,strong)NSMutableArray *nameArray;


@end

@implementation ZTRecommendViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor yellowColor];
    
    [self getTableView];
    [self getData];
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    _imagesArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < 20; ++ i) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:IMAGENAMED(SNOW_IMAGENAME)];
        float x = IMAGE_WIDTH;
        imageView.frame = CGRectMake(IMAGE_X, -30, x, x);
        imageView.alpha = IMAGE_ALPHA;
        [self.view addSubview:imageView];
        [_imagesArray addObject:imageView];
    }
    [NSTimer scheduledTimerWithTimeInterval:.3 target:self selector:@selector(makeSnow) userInfo:nil repeats:YES];

    

}
static int i = 0;
- (void)makeSnow
{
    i = i + 1;
    if ([_imagesArray count] > 0) {
        UIImageView *imageView = [_imagesArray objectAtIndex:0];
        imageView.tag = i;
        [_imagesArray removeObjectAtIndex:0];
        [self snowFall:imageView];
    }
    
}

- (void)snowFall:(UIImageView *)aImageView
{
    [UIView beginAnimations:[NSString stringWithFormat:@"%ld",aImageView.tag] context:nil];
    [UIView setAnimationDuration:6];
    [UIView setAnimationDelegate:self];
    aImageView.frame = CGRectMake(aImageView.frame.origin.x, Main_Screen_Height, aImageView.frame.size.width, aImageView.frame.size.height);
    //    NSLog(@"%@",aImageView);
    [UIView commitAnimations];
}

- (void)addImage
{
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    UIImageView *imageView = (UIImageView *)[self.view viewWithTag:[animationID intValue]];
    float x = IMAGE_WIDTH;
    imageView.frame = CGRectMake(IMAGE_X, -30, x, x);
    [_imagesArray addObject:imageView];
}


//创建表
-(void)getTableView
{
    
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, TZWIDTH, TZHEIGHT - 64 - 49 - 40) style:UITableViewStylePlain];
    
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    self.myTableView.estimatedRowHeight = 350;
    [self.view addSubview:self.myTableView];
//     self.myTableView.sectionHeaderHeight = TZWIDTH * 0.15;
//    self.myTableView.backgroundColor = [UIColor yellowColor];
    //设置尾部区域的大小
    self.myTableView.sectionFooterHeight = TZWIDTH *0.038;
    
    //轮播图
    [self creatHeader];
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    //初始化字典
    self.editor = [NSMutableDictionary dictionary];
    self.jingPin = [NSMutableDictionary dictionary];
    self.FindNew = [NSMutableDictionary dictionary];
    self.hotTuiJian = [NSMutableDictionary dictionary];
    
    //区名数组
    self.nameArray = [NSMutableArray array];
    
    
    
    
    //注册cell-editor
    [self.myTableView registerClass:[TZeditorViewCell class] forCellReuseIdentifier:@"TZeditorViewCell"];
    
    
    
    //-jingpin
    [self.myTableView registerClass:[TZJIngPinViewCell class] forCellReuseIdentifier:@"TZJIngPinViewCell"];
    
    //-find
    [self.myTableView registerClass:[TZfindViewCell class] forCellReuseIdentifier:@"TZfindViewCell"];
    
     //-hotTuiJian
    [self.myTableView registerClass:[TZFIndTableViewCell class] forCellReuseIdentifier:@"TZFIndTableViewCell"];
    

}


-(void)creatHeader
{

    self.headr = [[TZRommendHeader alloc]init];
    self.headr.frame = CGRectMake(0, 0, TZWIDTH, 220);
    self.headr.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.headr];
    self.myTableView.tableHeaderView = self.headr;
    


}
- (void)getData{
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=and-d10&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.3.26.2"];
    [AppTools getDataWithURL:url Block:^(id result) {
        
        NSMutableDictionary *Dic = result;
        NSMutableDictionary *LunBODic = Dic[@"focusImages"];
//        NSLog(@"LunBODic = %@",LunBODic);
        //重写set方法给轮播图赋值
        self.headr.dic = LunBODic;
        
        
        //给推荐主体传字典
        self.editor = Dic[@"editorRecommendAlbums"];
        self.jingPin = Dic[@"specialColumn"];
        self.FindNew = Dic[@"discoveryColumns"];
        self.hotTuiJian = Dic[@"hotRecommends"];
        NSString *Editor = [self.editor objectForKey:@"title"];
        NSString *jingPin = [self.jingPin objectForKey:@"title"];
        NSString *find = [self.FindNew objectForKey:@"title"];
        NSString *hot = [self.hotTuiJian objectForKey:@"title"];
        [self.nameArray addObject:Editor];
        [self.nameArray addObject:jingPin];
        [self.nameArray addObject:find];
        [self.nameArray addObject:hot];
        //刷新表 使得协议方法有值
        [self.myTableView reloadData];
        

        
    } ErrorBlock:^(NSError *error) {
        
        
    }];
}







//tableView 协议方法

//区头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.nameArray.count == 0) {
        return nil;
    }
    else{
    NSString *Str = [self.nameArray objectAtIndex:section];
    TZreommandTou *headerView = [[TZreommandTou alloc] init];
    headerView.titleLabel.text = Str;
    headerView.arrowImageView.image = [UIImage imageNamed:@"TzeditorFu.png"];
//    headerView.backgroundColor = [UIColor redColor];
        
//    [headerView.moreButton addTarget:self action:@selector(moreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return headerView;
    }
}





//区尾
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{

    UIView *footView = [[UIView alloc]init];
    footView.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.91 alpha:1];
    return footView;
    
}



//tableView 协议方法


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section == 3) {
        return 0;
        
    }
    else
    {
    return TZWIDTH * 0.11;
    }

}



#pragma mark 设置tableview区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;

}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    return 1;



}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return TZWIDTH *0.5;
    }
   else if (indexPath.section == 1) {
        return 170;
    }
    else if (indexPath.section == 2)
    {
    
        return 400;
    
    }
    else
        // itm + 头+ 尾巴
        
        return TZWIDTH *0.5*13 + TZWIDTH * 0.11*13 +60 + TZWIDTH *0.05*13 ;
    ;
   

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        static NSString *TZeditorViewCellID = @"TZeditorViewCell";
        TZeditorViewCell *TZeditorViewCell = [tableView dequeueReusableCellWithIdentifier:TZeditorViewCellID];
        TZeditorViewCell.picTableCellDelegate = self;
        TZeditorViewCell.myDic = self.editor;
        return TZeditorViewCell;
        
    }
    else if (indexPath.section == 1)
    {
    static NSString *TZJIngPinViewCellID = @"TZJIngPinViewCell";
        TZJIngPinViewCell *TZJIngPinViewCell = [tableView dequeueReusableCellWithIdentifier:TZJIngPinViewCellID];
        TZJIngPinViewCell.jingpinDeleget = self;
        TZJIngPinViewCell.myDic = self.jingPin;
        
//        TZJIngPinViewCell.backgroundColor = [UIColor redColor];
        return TZJIngPinViewCell;
    
    
    }
    else if (indexPath.section == 2)
    {
       
        static NSString *TZfindViewCellID = @"TZfindViewCell";
        TZfindViewCell *TZfindViewCell = [tableView dequeueReusableCellWithIdentifier:TZfindViewCellID];
        TZfindViewCell.findDeleget = self;
        TZfindViewCell.myDic = self.FindNew;
        TZfindViewCell.userInteractionEnabled = YES;
        return TZfindViewCell;
    
    }
    //下面是热门 我打错了 写成FInd 了
    else
    {
        
        
    static NSString *TZFIndTableViewCellID = @"TZFIndTableViewCell";
    TZFIndTableViewCell *TZFIndTableViewCell = [tableView dequeueReusableCellWithIdentifier:TZFIndTableViewCellID];
        TZFIndTableViewCell.picTableCellDelegate = self;
        TZFIndTableViewCell.myDic = self.hotTuiJian;
//        TZFIndTableViewCell.backgroundColor = [UIColor yellowColor];
    return TZFIndTableViewCell;
    }
    return nil;
    

    
}

#pragma mark - picCell协议方法  跳转到专辑详情

-(void)JumpToAlbumView:(NSString *)albumId :(NSString *)myTitle :(NSString *)trackId :(TZeditorModel *)editorModel
{
    TZEditorViewController *tzDetailVC = [[TZEditorViewController alloc]init];
//    tzDetailVC.hidesBottomBarWhenPushed = YES;

    tzDetailVC.albumId = albumId;
    tzDetailVC.myTitle = myTitle;
    [self.navigationController pushViewController:tzDetailVC animated:YES];
    

   
   


}

//跳转发现WEB详情 
-(void)pushtoFindDetail:(NSString *)webUrl
{

    if (webUrl.length > 0) {
        TZfindController *findetail = [[TZfindController alloc]init];
        findetail.webURL = webUrl;
        NSLog(@"webURL--%@",webUrl);
        
        //    findetail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:findetail animated:YES];

    } else {
        
    }
   

}


//跳转到精品详情
-(void)pushToJingPinDetail:(NSString *)specialId
{
    TZjingPInDetail *jingPin = [[TZjingPInDetail alloc]init];
//    jingPin.hidesBottomBarWhenPushed = YES;
    jingPin.spicId = specialId;
    [self.navigationController pushViewController:jingPin animated:YES];
    



}




//  区头不悬停


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat sectionHeaderHeight = TZWIDTH * 0.11;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if (scrollView.contentOffset.y>=sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
